package com.qf.contract.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.common.base.exception.BaseException;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.ResultCode;
import com.qf.common.db.utils.FileUploadUtils;
import com.qf.contract.entity.FChangeRequest;
import com.qf.contract.mapper.FChangeRequestMapper;
import com.qf.contract.qo.FChangeRequestQo;
import com.qf.contract.service.FChangeRequestService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 合同变更,
 * 水接口
 */
@Service
public class FChangeRequestServiceImpl implements FChangeRequestService {
    @Resource
    FChangeRequestMapper fChangeRequestMapper;
    @Resource
    FileUploadUtils fileUploadUtils;

    /**
     * 添加变更申请
     * 文件上传
     *
     * @param fChangeRequestQo
     * @param file
     * @return
     */
    @Override
    public int addFChangeRequest(FChangeRequestQo fChangeRequestQo, MultipartFile file) {
        //通过工具类，qo转po
        try {
            FChangeRequest fChangeRequest = new FChangeRequest();
            BeanUtils.copyProperties(fChangeRequestQo, fChangeRequest);
            if (file != null) {
                String imgStar = fileUploadUtils.uploadImage(file);
                if (!ObjectUtils.isEmpty(imgStar)) {
                    fChangeRequest.setRequestappendicesOfAContract(imgStar);
                } else {
                    throw new ServiceException(ResultCode.IMG_NOT_EXIST);
                }
            }
            //获取文件名

            //给对象赋值文件名
            //插入数据
            return fChangeRequestMapper.insert(fChangeRequest);
        } catch (
                RuntimeException e) {
            throw new BaseException(ResultCode.IMG_NOT_EXIST);
        } catch (
                Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }

    }

    /**
     * 查询申请表，单表操作 分页查询
     */
    @Override
    public Page<FChangeRequest> listFChangeRequest(int current, int size) {
        try {
            Page<FChangeRequest> fChangeRequestPage = fChangeRequestMapper.selectPage(new Page<>(current, size), null);
            return fChangeRequestPage;
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }

    /**
     * 删除表
     */
    @Override
    public int delFChangeRequest(int id) {
        try {
            return fChangeRequestMapper.deleteById(id);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }

    /**
     * 修改表
     */
    @Override
    public int updateFChangeRequest(FChangeRequest fChangeRequest) {
        try {

            return fChangeRequestMapper.updateById(fChangeRequest);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }


}
